<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_sms_main extends sms_main{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($sms_id='') {
		$detail = array();
        $sms_id = isset($_REQUEST['sms_id']) ? $_REQUEST['sms_id'] : '';
		$detail = $this->one_info('sms_main','sms_id',$sms_id);
        //DEBUG 调试输出
        include template('sms/main/detail');
	}
    
	/*
	*  发送短信消息验证码
	*/
	public function do_send() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$sms_main = isset($_REQUEST['sms_main']) && !empty($_REQUEST['sms_main']) ? $_REQUEST['sms_main']:'';
        if($sms_main['phone']){
            $sms_main['sms_phone'] = $sms_main['phone'];
            unset($sms_main['phone']);   
        }
		//TODO 后端字段校验 根据具体业务逻辑添加
        $sms_main['sms_code']=random(6,1);
        $sms_main['create_dateline'] = TIMESTAMP;
        $insert_id = $this->add('sms_main',$sms_main);
        if($insert_id){
            //DEBUG 发送SMS短信
            $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            $post_data = "account=cf_baifenpeixun&password=csbfpxzx147258&mobile=".$sms_main['sms_phone']."&content=".rawurlencode("您的验证码是：". $sms_main['sms_code']."。请不要把验证码泄露给其他人。");
            //密码可以使用明文密码或使用32位MD5加密
            $return_curl = @dzf_curl_post($post_data, $target);
            $return_curl_array = dzf_xml_to_array($return_curl);
            /*
             * $return_curl_array['SubmitResult']['code']
             * [code] => 403  [msg] => 手机号码不能为空
             * [code] => 2    [msg] => 提交成功
             * [code] => 406  [msg] => 手机格式不正确，格式为11位的数字
             * [code] => 4085 [msg] => 同一手机号验证码短信发送超出5条
             */
            if(2==$return_curl_array['SubmitResult']['code'] || '2'==$return_curl_array['SubmitResult']['code']){
                $return_curl_array['SubmitResult']['code']='0';
            }
            $return['code'] = $return_curl_array['SubmitResult']['code'];
            $return['data']=array('sms_id'=>$insert_id);
        }else{
            $return['code'] = '1';
        }
        echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
	}
    
    /*
	*  验证短信消息验证码
	*/
	public function do_check($mod='',$action='',$do='') {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$sms_main = isset($_REQUEST['sms_main']) && !empty($_REQUEST['sms_main']) ? $_REQUEST['sms_main']:'';
		//TODO 后端字段校验 根据具体业务逻辑添加
        $sms_id='';
        if(!empty($sms_main['sms_id']) && !empty($sms_main['sms_code'])){
            $active_time = TIMESTAMP - 300;//DEBUG 校验有效时间
            $sql = "SELECT sms_id,sms_phone,sms_code FROM ".DB::table('sms_main')." WHERE sms_id='".$sms_main['sms_id']."' AND sms_code='".$sms_main['sms_code']."' AND create_dateline > '".$active_time."' LIMIT 1";
            $sms_main_check = DB::fetch_first($sql);   
        }
        if($sms_main_check['sms_id']){
            $return['code'] = '0';
            $return['data']['sms_main']=$sms_main_check;
        }else{
            if(empty($sms_main['sms_id'])){
                $return['code'] = '2';
            }else{
                $return['code'] = '1';
            }
        }
        if($mod && $action && $do){
            return format_data($return,$_G['gp_api'],$mod,$action,$do);
        }else{
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$sms_main = isset($_REQUEST['sms_main']) && !empty($_REQUEST['sms_main']) ? $_REQUEST['sms_main']:'';
        $sms_id = isset($_REQUEST['sms_id']) ? $_REQUEST['sms_id'] : '';
        if($sms_id){
            if('1'==$issubmit){
                $sms_main['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($sms_main AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $sms_main[$key] = implode(",", $value);
                    }
                }                
                $where = array('sms_id'=>$sms_id);
                $effect_row = $this->edit('sms_main',$sms_main,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=sms&action=main&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=sms&action=main&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('sms_main','sms_id',$sms_id);
                include template('sms/main/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=sms&action=main&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function do_delete() {
        global $_G;
        $sms_id = isset($_REQUEST['sms_id']) ? $_REQUEST['sms_id'] : '';
        if($sms_id){
            $effect_row = $this->delete("sms_main", array("sms_id" => $sms_id), $limit=1);
            if($effect_row){
                showmessage('操作成功','index.php?mod=sms&action=main&do=index');
            }else{
                showmessage('操作失败','index.php?mod=sms&action=main&do=index');
            }
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND sms_phone LIKE '%".$keyword."%' ";
        }
        $orderby = " ORDER BY sms_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('sms_main',$page_condition);
        $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=sms&action=main&do=index&keyword=".$keyword);
		include template('sms/main/index');	
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY sms_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('sms_main',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
    
}
?>